//Hokie Sensor Help Window UI.
//Modified By David Park
/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

package org.vt.eldercare.ui;

import java.awt.Dimension;
import java.io.IOException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import javax.swing.JTree;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import org.vt.eldercare.ElderCareGUI;

public class HelpWindow extends JPanel
								implements TreeSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JEditorPane htmlPane;
	private JTree tree;
	private URL helpURL;
	
	public HelpWindow() {
	
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Help and Support");
		createNodes(root);
		
		tree = new JTree(root);
		tree.getSelectionModel().setSelectionMode
        (TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.setShowsRootHandles(true);
		
		tree.addTreeSelectionListener(this);
	
		
		JScrollPane treeView = new JScrollPane(tree);
		
		htmlPane = new JEditorPane();
		htmlPane.setEditable(false);
        htmlPane.setContentType("text/html");
        htmlPane.addHyperlinkListener(new HyperlinkListener()
        {
            public void hyperlinkUpdate(HyperlinkEvent r)
            {
                try
                {
             if(r.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
             htmlPane.setPage(r.getURL());
                }catch(Exception e)
                {}
            }
        });
		initPage();
		JScrollPane htmlView = new JScrollPane(htmlPane);
		
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        splitPane.setLeftComponent(treeView);
        splitPane.setRightComponent(htmlView);
        splitPane.setOneTouchExpandable(true);
        Dimension minimumSize = new Dimension(150, 100);
        htmlView.setMinimumSize(minimumSize);
        treeView.setMinimumSize(minimumSize);
        splitPane.setDividerLocation(175); 
        splitPane.setPreferredSize(new Dimension(1000, 600));
        splitPane.setResizeWeight(0.5);
        
        add(splitPane);
		
		
	}
	
	private void initPage() {
		
		String firstPage ="Contents.html";
		helpURL = ElderCareGUI.class.getResource(firstPage);
		
        try {
	htmlPane.setPage(helpURL);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	private void createNodes(DefaultMutableTreeNode root) {
	
		DefaultMutableTreeNode child=null;
		//DefaultMutableTreeNode grandChild;
		//DefaultMutableTreeNode greatGrandChild;
		
		child = new DefaultMutableTreeNode(new HelpInfo("Installation","Installation.html"));
		root.add(child);
		child = new DefaultMutableTreeNode(new HelpInfo("Menu Options","MenuCommands.html"));
		root.add(child);
		child = new DefaultMutableTreeNode(new HelpInfo("Graphical Elements","Graphical Elements.html"));
		root.add(child);
		child = new DefaultMutableTreeNode(new HelpInfo("Known Problems","Questions.html"));
		root.add(child);
		
	}

	public void valueChanged(TreeSelectionEvent e) {
		
	
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
		if(node == null) 
			return;
	
	
		Object nodeInfo = node.getUserObject();
		
		if(node.isLeaf()) {
			HelpInfo info = (HelpInfo)nodeInfo;
			displayURL(info.url);
		}
		else {
			displayURL(helpURL);
		}


}
private void displayURL(URL url) {
	try {
		if (url != null)
			htmlPane.setPage(url);
		 else {
		  htmlPane.setText("File Not Found");
		}
	} catch(IOException e) {
		System.err.println("Attempted to reade a bad URL: " + url);
	}
	
}
	
public static void createWindow() {
	JFrame frame = new JFrame("Help and Support");
	frame.add(new HelpWindow());
	frame.setMinimumSize(new Dimension(520,620));
	frame.setVisible(true);
	frame.pack();
	
}
private class HelpInfo{

	public String Title;
	public URL url;
	
	


	
	public HelpInfo(String s, String filename) {
		Title = s;
		url = ElderCareGUI.class.getResource(filename);
	}
	@Override
	public String toString() {
		return this.Title;
	}
}

}

